package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.ChatMapper;
import com.lxk.pojo.Chat;
import com.lxk.pojo.ChatExample;
import com.lxk.service.ChatService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.ChatGeneralModel;
import com.lxk.vo.ChatShowModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ChatServiceImpl implements ChatService {

	@Autowired
	private AsMyMapper mm;
	@Autowired
	private ChatMapper cm;
	
	public Integer getTotalPages(Integer num, Integer wxUserId) {
		ChatExample example = new ChatExample();
		ChatExample.Criteria criteria = example.createCriteria();
		criteria.andWxUserIdEqualTo(wxUserId);
		Integer count = (int)cm.countByExample(example);
		int totalPages = count%num==0 ? count/num : count/num+1;
		return totalPages;
	}

	public PageModel<ChatShowModel> getChatList(Integer pageIndex, Integer num, Integer totalPages, Integer wxUserId) {
		int start = (pageIndex-1)*num;
		List<ChatShowModel> list = mm.selChatList(start, num, wxUserId);
		PageModel<ChatShowModel> model = new PageModel<ChatShowModel>();
		model.setList(list);
		model.setNum(num);
		model.setPageIndex(pageIndex);
		model.setTotalPages(totalPages);
		return model;
	}

	public void setUserChatRead(Integer wxUserId) {
		ChatExample example = new ChatExample();
		ChatExample.Criteria criteria = example.createCriteria();
		criteria.andWxUserIdEqualTo(wxUserId).andChatRoleEqualTo(0);
		Chat chat = new Chat();
		chat.setChatStat(1);
		int upd = cm.updateByExampleSelective(chat, example);
	}

	public void addChat(String chatText, Integer wxUserId) {
		if(chatText==null||chatText.equals("")) {
			throw new GlobalException(CodeMsg.INFO_NOT_EXIST);
		}
		Chat chat = new Chat();
		chat.setChatRole(1);
		chat.setChatStat(0);
		chat.setChatText(chatText);
		chat.setChatTime(new Date());
		chat.setWxUserId(wxUserId);
		int ins = cm.insertSelective(chat);
		if(ins==0) {
			throw new GlobalException(CodeMsg.INFO_NOT_EXIST);
		}
	}

	public Integer getUnreadCount() {
		Integer count = mm.countUnreadChat();
		return count;
	}

	public Integer getUnreadTotalPages(Integer num) {
		Integer count = mm.countUnreadChat();
		int totalPages = count%num==0 ? count/num : count/num+1;
		return totalPages;
	}

	public PageModel<ChatGeneralModel> getUnreadChatList(Integer pageIndex, Integer num, Integer totalPages) {
		int start = (pageIndex-1)*num;
		List<ChatGeneralModel> list = mm.getUnreadList(start, num);
		PageModel<ChatGeneralModel> model = new PageModel<ChatGeneralModel>();
		model.setList(list);
		model.setNum(num);
		model.setPageIndex(pageIndex);
		model.setTotalPages(totalPages);
		return model;
	}

	public Integer getAllTotalPages(Integer num) {
		Integer count = mm.countAllChat();
		int totalPages = count%num==0 ? count/num : count/num+1;
		return totalPages;
	}

	public PageModel<ChatGeneralModel> getAllChatList(Integer pageIndex, Integer num, Integer totalPages) {
		int start = (pageIndex-1)*num;
		List<ChatGeneralModel> list = mm.getAllList(start, num);
		PageModel<ChatGeneralModel> model = new PageModel<ChatGeneralModel>();
		model.setList(list);
		model.setNum(num);
		model.setPageIndex(pageIndex);
		model.setTotalPages(totalPages);
		return model;
	}


}
